package host.haiwai.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import host.haiwai.entity.Admin;
import host.haiwai.mapper.AdminMapper;
import host.haiwai.mapstruct.AdminMapstruct;
import host.haiwai.vo.AdminVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author joefche / JC
 * @since 2021-08-29
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminMapper adminMapper;

    /** layui 动态表格数据格式
     * {
     *   "code": 0,
     *   "msg": "",
     *   "count": 1000,
     *   "data": [
     *     {
     *       "adminId": 10000,
     *       "adminName": "user-0",
     *       "roleName": "女",
     *       "city": "城市-0",
     *       "sign": "签名-0",
     *       "experience": 255,
     *       "logins": 24,
     *       "wealth": 82830700,
     *       "classify": "作家",
     *       "score": 57
     *     }
     *   ]
     * }
     *
     * page     当前页面
     * limit    显示条数
     * @return
     */

    //模板显示
    @GetMapping("/userList")
    public String userList() {
        return "user/userList";
    }

    //会员数据列表
    @RequestMapping("/jsonList")
    @ResponseBody
    public Object jsonList(@RequestParam(defaultValue = "1") Integer page ,
                           @RequestParam(defaultValue = "1") Integer limit,
                           @RequestParam(defaultValue = "") String searchObject ) {
        QueryWrapper wrapper = new QueryWrapper<>();
        AdminVo adminQuery = JSON.parseObject(searchObject, AdminVo.class);

        if(adminQuery != null) wrapper.like("username",adminQuery.getUsername());

        Page<Admin> pageData = new Page<>(page,limit);
        Page<Admin> admin = adminMapper.selectPage(pageData, wrapper);
        List<Admin> adminList = admin.getRecords();
        List<AdminVo> adminVoList = AdminMapstruct.mapper.adminList2Vo(adminList);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",admin.getTotal());
        map.put("data",adminVoList);
        return JSON.toJSON(map);

    }

    //显示修改会员页面
    @GetMapping("/userEdit")
    public ModelAndView userEdit(@RequestParam(defaultValue = "0") Integer id,ModelAndView modelAndView) {
        modelAndView.addObject("id",id);
        modelAndView.setViewName("/user/userEdit");
        return modelAndView;
    }

    //更新会员资料
    @PostMapping("/userEdit")
    @ResponseBody
    public Object userEdit(Admin admin) {
        HashMap<Object, Object> map = new HashMap<>();
        //如果密码为空，则设置为 null ， 不修改任何操作
        if(admin.getPassword().equals("")) admin.setPassword(null);

        int i = adminMapper.updateById(admin);

        if(i > 0) {
            map.put("code",1);
            map.put("data","操作成功");
        } else {
            map.put("code", 2);
            map.put("data", "操作失败");
        }
        return JSON.toJSON(map);
    }

    //获取指定会员资料
    @GetMapping("/getUserById")
    @ResponseBody
    public Object getUserById(@RequestParam(defaultValue = "0") Integer id) {
        Admin admin = adminMapper.selectById(id);
        AdminVo adminVo = AdminMapstruct.mapper.adminPojo2Vo(admin);
        return JSON.toJSON(adminVo);
    }

    //显示新增会员页面
    @GetMapping("/userAdd")
    public ModelAndView userAdd(ModelAndView modelAndView) {
        modelAndView.setViewName("/user/userAdd");
        return modelAndView;
    }

    //新增会员操作
    @PostMapping("/userAdd")
    @ResponseBody
    public Object userAdd(Admin admin) {
        HashMap<Object, Object> map = new HashMap<>();
        int i = adminMapper.insert(admin);
        if(i > 0) {
            map.put("code",1);
            map.put("data","操作成功");
        } else {
            map.put("code", 2);
            map.put("data", "操作失败");
        }
        return JSON.toJSON(map);
    }

    //删除用户操作
    @GetMapping("/deleteUser")
    @ResponseBody
    public Object deleteUser(@RequestParam(defaultValue = "0") Integer id) {
        HashMap<Object, Object> map = new HashMap<>();
        int i = adminMapper.deleteById(id);
        if(i > 0) {
            map.put("code",1);
            map.put("data","操作成功");
        } else {
            map.put("code", 2);
            map.put("data", "操作失败");
        }
        return JSON.toJSON(map);
    }
}

